import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<SubjectEditState> buildReducer() {
  return asReducer(
    <Object, Reducer<SubjectEditState>>{
      SubjectEditAction.selectType: _selectType,
      SubjectEditAction.edit: _edit,
    },
  );
}

SubjectEditState _selectType(SubjectEditState state, Action action) {
  return state.clone()..subject.articleTypeList.add(action.payload);
}

SubjectEditState _edit(SubjectEditState state, Action action) {
  Map<String, dynamic> values = action.payload;
  var newState = state.clone();

  values.forEach((key, value) {
    switch (key) {
      case 'title':
        newState.subject.title = value;
        break;
      case 'demand':
        newState.subject.demand = value;
        break;
      case 'ideation':
        newState.subject.ideation = value;
        break;
      case 'keywords':
        newState.subject.keywordList = value.split(' ');
        break;
      case 'type':
        newState.subject.articleTypeList = value;
        break;
      case 'grade':
        newState.subject.gradeList = value;
        break;
      case 'tagList':
        newState.subject.tagList = value;
        break;
      case 'themeList':
        newState.subject.themeList = value;
        break;
      default:
    }
  });

  return newState;
}
